﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Printing;
using System.Drawing.Imaging;
using System.Diagnostics;
using System.Collections;
using System.IO;
using System.Threading;
using System.Runtime.InteropServices;
using System.Windows.Forms;


namespace KWMK.Windows.WinApi
{
    /// <summary>
    ///  User32.dll 的辅助类。
    /// </summary>
    /// <remarks>主要包括一些User32.dll用到的操作系统SDK</remarks>
    public static class User32
    {
        #region 消息函数


        /// <summary>
        /// 容许接受窗体切换为前台
        /// </summary>
        public const uint BSF_ALLOWSFW = 0x00000080;
        /// <summary>
        /// 发送之后刷新磁盘
        /// </summary>
        public const uint BSF_FLUSHDISK = 0x00000004;
        /// <summary>
        /// 失败继续
        /// </summary>
        public const uint BSF_FORCEIFHUNG = 0x00000020;
        /// <summary>
        /// 忽略当前进程的窗口
        /// </summary>
        public const uint BSF_IGNORECURRENTTASK = 0x00000002;
        /// <summary>
        /// 失败停止
        /// </summary>
        public const uint BSF_NOHANG = 0x00000008;
        /// <summary>
        /// 无限超时设置
        /// </summary>
        public const uint BSF_NOTIMEOUTIFNOTHUNG = 0x00000040;
        /// <summary>
        /// 异步发送，不能和同步并用
        /// </summary>
        public const uint BSF_POSTMESSAGE = 0x00000010;
        /// <summary>
        /// 同步发送，成功继续
        /// </summary>
        public const uint BSF_QUERY = 0x00000001;
        /// <summary>
        /// 使用SendNotifyMessage函数发送，不能和同步并用
        /// </summary>
        public const uint BSF_SENDNOTIFYMESSAGE = 0x00000100;

        /// <summary>
        /// 全部
        /// </summary>
        public const uint BSM_ALLCOMPONENTS = 0x00000000;
        /// <summary>
        /// 桌面
        /// </summary>
        public const uint BSM_ALLDESKTOPS = 0x00000010;
        /// <summary>
        /// 应用程序
        /// </summary>
        public const uint BSM_APPLICATIONS = 0x00000008;
        /// <summary>
        /// Win98
        /// </summary>
        public const uint BSM_INSTALLABLEDRIVERS = 0x00000004;
        /// <summary>
        /// Win98
        /// </summary>
        public const uint BSM_NETDRIVER = 0x00000002;
        /// <summary>
        /// Win98
        /// </summary>
        public const uint BSM_VXDS = 0x00000001;

        /// <summary>
        /// 发送广播
        /// </summary>
        /// <param name="dwFlags">标志</param>
        /// <param name="lpdwRecipients">过滤</param>
        /// <param name="uiMessage">消息</param>
        /// <param name="wParam">参数1</param>
        /// <param name="lParam">参数2</param>
        /// <returns>-1表示出错</returns>
        [DllImport("User32.dll")]
        public static extern long BroadcastSystemMessage(uint dwFlags, ref uint lpdwRecipients, uint uiMessage, uint wParam, uint lParam);

        /// <summary>
        /// 异步发送消息
        /// </summary>
        /// <param name="hWnd">句柄</param>
        /// <param name="Msg">消息</param>
        /// <param name="wParam">参数1</param>
        /// <param name="lParam">参数2</param>
        /// <returns>是否</returns>
        [DllImport("User32.dll")]
        public static extern bool PostMessage(IntPtr hWnd, uint Msg, uint wParam, uint lParam);

        /// <summary>
        /// 异步发送线程消息
        /// </summary>
        /// <param name="idThread">句柄</param>
        /// <param name="Msg">消息</param>
        /// <param name="wParam">参数1</param>
        /// <param name="lParam">参数2</param>
        /// <returns>是否</returns>
        [DllImport("User32.dll")]
        public static extern bool PostThreadMessage(uint idThread, uint Msg, uint wParam, uint lParam);

        /// <summary>
        /// 同步发送消息
        /// </summary>
        /// <param name="hWnd">句柄</param>
        /// <param name="Msg">消息</param>
        /// <param name="wParam">参数1</param>
        /// <param name="lParam">参数2</param>
        /// <returns>返回值</returns>
        [DllImport("User32.dll")]
        public static extern uint SendMessage(IntPtr hWnd, uint Msg, uint wParam, uint lParam);


        /// <summary>
        /// 发送消息
        /// </summary>
        /// <param name="hWnd">句柄</param>
        /// <param name="Msg">消息</param>
        /// <param name="wParam">参数1</param>
        /// <param name="lParam">参数2</param>
        /// <returns>是否</returns>
        [DllImport("User32.dll")]
        public static extern bool SendNotifyMessage(IntPtr hWnd, uint Msg, uint wParam, uint lParam);
        #endregion
        #region Windows 窗口函数


        /// <summary>
        /// 桌面是否有窗口
        /// </summary>
        /// <returns>是否</returns>
        [DllImport("User32.dll")]
        public static extern bool AnyPopup();


        /// <summary>
        /// 窗口移到前面
        /// </summary>
        /// <returns>是否</returns>
        [DllImport("User32.dll")]
        public static extern bool BringWindowToTop(IntPtr hWnd);


        /// <summary>
        /// 全部
        /// </summary>
        public const uint CWP_ALL = 0;
        /// <summary>
        /// 排除不显示的
        /// </summary>
        public const uint CWP_SKIPINVISIBLE = 1;
        /// <summary>
        /// 排除禁用的
        /// </summary>
        public const uint CWP_SKIPDISABLED = 2;
        /// <summary>
        /// 排除透明的
        /// </summary>
        public const uint CWP_SKIPTRANSPARENT = 4;


        /// <summary>
        /// 指定点的子窗口
        /// </summary>
        /// <param name="hwndParent">父窗口</param>
        /// <param name="pt">相对坐标点</param>
        /// <param name="uFlags">标志</param>
        /// <returns>句柄</returns>
        [DllImport("User32.dll")]
        public static extern IntPtr ChildWindowFromPointEx(IntPtr hwndParent, Point pt, uint uFlags);


        /// <summary>
        /// 窗口关闭
        /// </summary>
        /// <returns>是否</returns>
        [DllImport("User32.dll")]
        public static extern bool CloseWindow(IntPtr hWnd);


        /// <summary>
        /// 窗口析构
        /// </summary>
        /// <returns>是否</returns>
        [DllImport("User32.dll")]
        public static extern bool DestroyWindow(IntPtr hWnd);


        /// <summary>
        /// 查找窗口
        /// </summary>
        /// <param name="hwndParent">父句柄，空为桌面</param>
        /// <param name="hwndChildAfter">子句柄，找它之后的，空表示第一个</param>
        /// <param name="lpszClass">类型名称</param>
        /// <param name="lpszWindow">标题</param>
        /// <returns>句柄，0没有找到</returns>
        [DllImport("User32.dll")]
        public static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);


        /// <summary>
        /// 查找父窗口
        /// </summary>
        public const uint GA_PARENT = 1;
        /// <summary>
        /// 查找根窗口
        /// </summary>
        public const uint GA_ROOT = 2;
        /// <summary>
        /// 查找父根窗口
        /// </summary>
        public const uint GA_ROOTOWNER = 3;


        /// <summary>
        /// 得到祖先
        /// </summary>
        /// <param name="hWnd">句柄</param>
        /// <param name="gaFlags">类型</param>
        /// <returns>句柄</returns>
        [DllImport("User32.dll")]
        public static extern IntPtr GetAncestor(IntPtr hWnd, uint gaFlags);


        /// <summary>
        /// 得到桌面句柄
        /// </summary>
        /// <returns>句柄</returns>
        [DllImport("User32.dll")]
        public static extern IntPtr GetDesktopWindow();


        /// <summary>
        /// 得到前台窗口句柄，无返回0
        /// </summary>
        /// <returns>句柄</returns>
        [DllImport("User32.dll")]
        public static extern IntPtr GetForegroundWindow();


        /// <summary>
        /// 得到刚才的前台窗口句柄
        /// </summary>
        /// <param name="hWnd">父窗体</param>
        /// <returns>句柄，无返回0</returns>
        [DllImport("User32.dll")]
        public static extern IntPtr GetLastActivePopup(IntPtr hWnd);


        /// <summary>
        /// 得到父窗口，无返回0
        /// </summary>
        /// <param name="hWnd">句柄</param>
        /// <returns>句柄</returns>
        [DllImport("User32.dll")]
        public static extern IntPtr GetParent(IntPtr hWnd);


        /// <summary>
        /// 得到桌面视图窗口，无返回0
        /// </summary>
        /// <returns>句柄</returns>
        [DllImport("User32.dll")]
        public static extern IntPtr GetShellWindow();


        /// <summary>
        /// 得到最前部窗口，无返回0
        /// </summary>
        /// <param name="hWnd">父窗体</param>
        /// <returns>句柄</returns>
        [DllImport("User32.dll")]
        public static extern IntPtr GetTopWindow(IntPtr hWnd);


        /// <summary>
        /// 得到窗口所属线程、进程，返回值为线程句柄，输出参数为进程句柄，
        /// </summary>
        /// <param name="hWnd">句柄</param>
        /// <param name="lpdwProcessId">进程句柄</param>
        /// <returns>线程句柄</returns>
        [DllImport("User32.dll")]
        public static extern uint GetWindowThreadProcessId(IntPtr hWnd, ref uint lpdwProcessId);


        /// <summary>
        /// 是否子窗口
        /// </summary>
        /// <param name="hWndParent">父亲、祖先句柄</param>
        /// <param name="hWnd">句柄</param>
        /// <returns>是否</returns>
        [DllImport("User32.dll")]
        public static extern bool IsChild(IntPtr hWndParent, IntPtr hWnd);


        /// <summary>
        /// 是否无响应
        /// </summary>
        /// <param name="hWnd">句柄</param>
        /// <returns>是否</returns>
        [DllImport("User32.dll")]
        public static extern bool IsHungAppWindow(IntPtr hWnd);


        /// <summary>
        /// 是否最小化
        /// </summary>
        /// <param name="hWnd">句柄</param>
        /// <returns>是否</returns>
        [DllImport("User32.dll")]
        public static extern bool IsIconic(IntPtr hWnd);


        /// <summary>
        /// 是否窗口
        /// </summary>
        /// <param name="hWnd">句柄</param>
        /// <returns>是否</returns>
        [DllImport("User32.dll")]
        public static extern bool IsWindow(IntPtr hWnd);


        /// <summary>
        /// 窗口是否可见
        /// </summary>
        /// <param name="hWnd">句柄</param>
        /// <returns>是否</returns>
        [DllImport("User32.dll")]
        public static extern bool IsWindowVisible(IntPtr hWnd);


        /// <summary>
        /// 最小化窗口还原
        /// </summary>
        /// <param name="hWnd">句柄</param>
        /// <returns>是否</returns>
        [DllImport("User32.dll")]
        public static extern bool OpenIcon(IntPtr hWnd);


        /// <summary>
        /// 得到窗口真实类名
        /// </summary>
        /// <param name="hWnd">句柄</param>
        /// <param name="pszType">缓冲区</param>
        /// <param name="cchType">大小</param>
        /// <returns>成功返回字符数，失败返回0</returns>
        [DllImport("User32.dll")]
        public static extern uint RealGetWindowClass(IntPtr hWnd, ref string pszType, uint cchType);


        /// <summary>
        /// 设置为前台窗口
        /// </summary>
        /// <param name="hWnd">句柄</param>
        /// <returns>是否</returns>
        [DllImport("User32.dll")]
        public static extern bool SetForegroundWindow(IntPtr hWnd);


        /// <summary>
        /// 设置父窗口
        /// </summary>
        /// <param name="hWndChild">儿子窗口句柄</param>
        /// <param name="hWndNewParent">新的父窗口句柄</param>
        /// <returns>原来的父窗口句柄</returns>
        [DllImport("User32.dll")]
        public static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);


        /// <summary>
        /// 隐藏
        /// </summary>
        public const uint SW_HIDE = 0;
        /// <summary>
        /// 正常
        /// </summary>
        public const uint SW_SHOWNORMAL = 1;
        /// <summary>
        /// 正常
        /// </summary>
        public const uint SW_NORMAL = 1;
        /// <summary>
        /// 最小化
        /// </summary>
        public const uint SW_SHOWMINIMIZED = 2;
        /// <summary>
        /// 最大化
        /// </summary>
        public const uint SW_SHOWMAXIMIZED = 3;
        /// <summary>
        /// 最大化
        /// </summary>
        public const uint SW_MAXIMIZE = 3;
        /// <summary>
        /// 显示不激活
        /// </summary>
        public const uint SW_SHOWNOACTIVATE = 4;
        /// <summary>
        /// 显示
        /// </summary>
        public const uint SW_SHOW = 5;
        /// <summary>
        /// 最小化
        /// </summary>
        public const uint SW_MINIMIZE = 6;
        /// <summary>
        /// 最小化不激活
        /// </summary>
        public const uint SW_SHOWMINNOACTIVE = 7;
        /// <summary>
        /// 显示不激活
        /// </summary>
        public const uint SW_SHOWNA = 8;
        /// <summary>
        /// 恢复原状，从最大化或最小化
        /// </summary>
        public const uint SW_RESTORE = 9;
        /// <summary>
        /// 按照窗口创建要求现实
        /// </summary>
        public const uint SW_SHOWDEFAULT = 10;
        /// <summary>
        /// 强制最小化，即使没有相应
        /// </summary>
        public const uint SW_FORCEMINIMIZE = 11;
        /// <summary>
        /// 同上
        /// </summary>
        public const uint SW_MAX = 11;


        /// <summary>
        /// 显示窗口
        /// </summary>
        /// <param name="hWnd">句柄</param>
        /// <param name="nCmdShow">命令</param>
        /// <returns>是否</returns>
        [DllImport("User32.dll")]
        public static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);


        /// <summary>
        /// 指定位置的窗口，不找不显示的和禁用的
        /// </summary>
        /// <param name="point">点</param>
        /// <returns>句柄</returns>
        [DllImport("User32.dll")]
        public static extern IntPtr WindowFromPoint(Point point);


        #endregion Windows 窗口函数

    }
}
